package com.youxin.cpcenter.controller;

import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.ent.RequestMemberIdentifyBean;
import com.youxin.busfoundation.bean.ent.RequestMemberInfoIdentify;
import com.youxin.busfoundation.bean.ent.RequestTrySubmitBean;
import com.youxin.busfoundation.bean.leads.LeadsStatTotalBean;
import com.youxin.cpcenter.bean.RequestTryoutList;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.dao.TbMemberInfoMapper;
import com.youxin.cpcenter.service.EnterpriseService;
import com.youxin.cpcenter.service.LoginService;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.RegexUtils;

/**
 * 企业功能接口
 * 
 * @author yingde.cao
 *
 */
@RestController
public class EnterpriseController extends BaseController {

	@Autowired
	private EnterpriseService enterpriseService;

	/**
	 * 试用提交表单
	 * 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id = "ent.tryout.submit", eventContent = {})
	@RequestMapping(value = "/api/tryout/submit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject tryoutSubmit(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			@RequestBody RequestTrySubmitBean bean) {
		try {
			ObjectUtils.requestNonNull(bean, "请填写参数");
			ObjectUtils.requestNonTrimEmpty(bean.getRealname(), "请填写真实姓名");
			ObjectUtils.requestNonTrimEmpty(bean.getCompanyName(), "请填写公司名称");
			ObjectUtils.requestNonTrimEmpty(bean.getPosition(), "请填写职位信息");
			ObjectUtils.requestNonTrimEmpty(bean.getCode(), "请填写验证码");
			if (RegexUtils.isMobile(bean.getMobile()) == false) {
				return ResponseObject.userFailure("请填写真实的手机号码");
			}
			return enterpriseService.tryoutSubmit(bean);
		} catch (DuplicateKeyException e) {
			logger.error("/api/tryout/submit错误:", e);
			return ResponseObject.userFailure("已经提交表单");
		} catch (Exception e) {
			logger.error("/api/tryout/submit异常", e);
			return ResponseObject.systemError(e);
		}
	}

	@LogMethod(id = "ent.member.oa.identify", eventContent = {})
	@RequestMapping(value = "/api/member/info/identify", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject memberIdentify(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			@RequestBody RequestMemberInfoIdentify bean) {
		try {
			ObjectUtils.requestNonNull(bean, "请填写参数");
			ObjectUtils.requestNonTrimEmpty(bean.getRealname(), "请填写真实姓名");
			ObjectUtils.requestNonTrimEmpty(bean.getCompanyName(), "请填写公司名称");
			ObjectUtils.requestNonTrimEmpty(bean.getCompanyUid(), "请填写公司UID");
			ObjectUtils.requestNonTrimEmpty(bean.getPosition(), "请填写职位信息");
			if (RegexUtils.isMobile(bean.getMobile()) == false) {
				return ResponseObject.userFailure("请填写真实的手机号码");
			}
			return enterpriseService.memberInfoIdentify(bean);
		} catch (DuplicateKeyException e) {
			logger.error("/api/tryout/submit错误:", e);
			return ResponseObject.userFailure("已经提交表单");
		} catch (Exception e) {
			logger.error("/api/member/info/identify异常", e);
			return ResponseObject.systemError(e);
		}
	}

	@LogMethod(id = "ent.member.identify.list", eventContent = {})
	@RequestMapping(value = "/api/member/identify/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject listAccount(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestMemberIdentifyBean bean) {
		try {
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			return enterpriseService.listIdentify(bean);
		} catch (Exception e) {
			logger.error("/api/member/identify/list异常", e);
			return ResponseObject.systemError(e);
		}
	}

	@Value("${server.port}")
	private String port;

	// @LogMethod(id="ent.yjw.test",eventContent={})
	@RequestMapping(value = "/yjw", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject test(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws Exception {
		String host = "";
		try {
			host = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
		}

		return new ResponseObject(host + ":" + port);
	}

}
